package com.caigou.modules.famous.ui.subscribe.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.famous.R
import com.caigou.modules.famous.adpter.SubscribeItemAdapter
import com.caigou.modules.famous.bean.SubscribeItemBean
import com.caigou.modules.famous.databinding.FragmentSubscribe2otherBinding
import com.caigou.modules.famous.ui.subscribe.FamousMySubscribeActivity
import com.caigou.modules.famous.viewmodel.SubscribeViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/10
 */
@AndroidEntryPoint
class Subscribe2otherFragment : BaseFragment() {

    private var _binding: FragmentSubscribe2otherBinding? = null
    private val binding get() = _binding!!

    private val subscribeViewModel: SubscribeViewModel by viewModels()

    private val mAdapter: SubscribeItemAdapter by lazy {
        SubscribeItemAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentSubscribe2otherBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.subscribe2otherSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchSubscribe2otherList()
            }
        }

        binding.subscribe2otherRecyclerView.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val item = mAdapter.getItem(position)
            ARouter.getInstance().build(RouterPath.Personal.PAGE_PERSONAL_PAGE)
                .withString(AppConstants.USER_ID, item.userId)
                .navigation()
        }

        LiveDataBus.get().with(BusKey.SUBSCRIBE_TYPE_CHANGE, Int::class.java).observe(this) {
            fetchSubscribe2otherList()
        }
    }

    override fun lazyLoad() {
        fetchSubscribe2otherList()
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(subscribeViewModel.subscribe2otherListLiveData, ::handleGetSubscribe2otherList)
    }

    private fun handleGetSubscribe2otherList(result: Resource<MutableList<SubscribeItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.subscribe2otherSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取订阅的人信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.subscribe2otherSwipeRefreshLayout.isRefreshing = false
                if (result.data == null) {
                    return
                }
                mAdapter.setList(result.data!!)
            }
        }
    }

    private fun fetchSubscribe2otherList() {
        val type = (requireActivity() as FamousMySubscribeActivity).famousType
        subscribeViewModel.getSubscribe2otherList(type, AppBackgroundInfo.instance.userBean!!.userId)
    }

}